package nuaa.controller;

import nuaa.domain.Administrator;
import nuaa.domain.Group;
import nuaa.domain.OrdinaryUser;
import nuaa.domain.vo.MessageModel;
import nuaa.service.GroupService;
import nuaa.service.UserService;
import nuaa.util.UtilUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    private UserService userService=new UserService();
    private GroupService groupService=new GroupService();
    public AdminController() throws IOException {
    }

    /**
     * 管理员登录
     * @param admin
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    @RequestMapping("/login")
    public void login(Administrator admin, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //1.接收客户端的请求（接收参数：邮箱、密码）
        //2.调用service层的方法，返回消息模型对象
        MessageModel messageModel = userService.userLogin(admin);
        //3.判断消息状态码
        if (messageModel.getCode()==1){ //成功
            //将消息模型中的用户信息设置到session作用域中，重定向到index.jsp。
            //使用更为普遍的名称，即user
            request.getSession().setAttribute("admin",messageModel.getObject());
            //将当前的对象设置到全局中
            UtilUser.setAdmin((Administrator)messageModel.getObject());
            //转发到admin.jsp页
            request.getRequestDispatcher("/WEB-INF/pages/admin.jsp").forward(request,response);
        }else{
            //将消息模型对象设置到request作用域中，请求转发到login.jsp
            request.setAttribute("messageModel",messageModel);
            //使用这个的时候，默认前提是 /user。前面加上一个 /，便可以了。
            request.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(request,response);
        }
    }

    /**ddddd
     * 进入发送公告的页面
     * @return
     */
    @RequestMapping("/announce")
    public String announce(){
        return "admin";
    }

    /**
     * 查询出队伍之后，可以删除队伍
     * @return
     */
    @RequestMapping("/deleteGroup")
    public String deleteGroup(){
        return "admin";
    }

    /**
     * 向index.jsp group.jsp recommend.jsp中写入公告
     * 其实，直接放到application作用域中就行
     */
    @RequestMapping("/postToIndex")
    public void postToIndex(String announceContent, HttpServletRequest request){
        //将announceContent放到application作用域中

        ServletContext context = request.getSession().getServletContext();
        context.setAttribute("announceContent",announceContent);
        System.out.println(announceContent);
    }

    /**
     * 搜索出游地
     * @param request
     * @param model
     * @return
     * @throws IOException
     */
    @RequestMapping("/adminSearch")
    public String adminSearch(HttpServletRequest request, Model model) throws IOException {
        //1.交给service来处理，获取到。获取到最后的展示结果，放到message中
        String searchGroup = request.getParameter("search-group");
        System.out.println("searchGroup:"+searchGroup);
        List<Group> groups = userService.adminSearch(searchGroup,request);
        System.out.println(groups);
        //2.将查找到的数据放到model中，前端可以取出
        model.addAttribute("searchedGroups",groups);
        return "adminSearchResult";
    }

    /**
     * 删除出游地
     */
    @RequestMapping("/delete")
    public String delete(Integer groupId) throws IOException {
        //调用groupService根据groupId删除出游地方法
        groupService.deleteGroupByGroupId(groupId);
        return "redirect:/adminInfo?pageNum=2";
    }
}
